package com.example.storage.controller;

import cn.hutool.core.date.DateField;
import com.example.starter.amazon.pojo.OssResponse;
import com.example.starter.amazon.template.OssTemplate;
import com.example.starter.util.Response;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 王令
 * @since 2022/11/6 11:07
 */
@RequiredArgsConstructor
@RestController
public class StorageController {

    private final OssTemplate ossTemplate;

    @PostMapping("/upload")
    public Response<OssResponse> upload(@RequestParam("file") MultipartFile multipartFile) {
        OssResponse response = ossTemplate.putObject(multipartFile);
        return Response.ok(response);
    }

    @GetMapping("/pre-sign")
    public Response<OssResponse> preSign(@RequestParam("filename") String filename) {
        OssResponse response = ossTemplate.generatePreSignedUrl(filename, 60, DateField.SECOND);
        return Response.ok(response);
    }

}
